package com.sy.simpleegg.room.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import com.sy.simpleegg.ext.logFile
import com.sy.simpleegg.room.dao.CashPackageDao
import com.sy.simpleegg.room.dao.CoinDao
import com.sy.simpleegg.room.dao.ControlBoardDao
import com.sy.simpleegg.room.dao.GoodDao
import com.sy.simpleegg.room.dao.OrderDao
import com.sy.simpleegg.room.dao.OutDao
import com.sy.simpleegg.room.dao.SaveCoinDao
import com.sy.simpleegg.room.entity.CashPackageEntity
import com.sy.simpleegg.room.entity.CoinEntity
import com.sy.simpleegg.room.entity.ControlBoardEntity
import com.sy.simpleegg.room.entity.GoodEntity
import com.sy.simpleegg.room.entity.OrderEntity
import com.sy.simpleegg.room.entity.OutEntity
import com.sy.simpleegg.room.entity.SaveCoinEntity

@Database(entities = [GoodEntity::class, OrderEntity::class, OutEntity::class, CoinEntity::class,
    CashPackageEntity::class, ControlBoardEntity::class, SaveCoinEntity::class], version = 1, exportSchema = false)
abstract class AppDatabase: RoomDatabase() {
    abstract fun goodDao(): GoodDao
    abstract fun orderDao(): OrderDao
    abstract fun outDao(): OutDao
    abstract fun coinDao(): CoinDao
    abstract fun cashPackageDao(): CashPackageDao

    abstract fun controlBoardDao(): ControlBoardDao

    abstract fun saveCoinDao(): SaveCoinDao

    companion object{
        private var instance: AppDatabase? = null

        fun get(context: Context): AppDatabase{
            if (instance == null){
                instance = Room.databaseBuilder(context, AppDatabase::class.java, "currency.db")
                    .fallbackToDestructiveMigration()
                    .allowMainThreadQueries()
                    .addCallback(object : Callback(){
                        override fun onCreate(db: SupportSQLiteDatabase) {
                            super.onCreate(db)
                            logFile("onCreate db_name is=" + db.path)
                        }
                    })
//                    .addMigrations(migration_1_2, migration_2_3, migration_1_3, migration_3_4, migration_2_4, migration_1_4)
                    .build()
            }
            return instance!!
        }

//        val migration_1_2: Migration = object : Migration(1, 2) {
//            override fun migrate(database: SupportSQLiteDatabase) {
//                database.execSQL("ALTER TABLE tb_order ADD COLUMN accountRemainCoin INTEGER NOT NULL DEFAULT 0")
//            }
//        }
//
//        val migration_2_3: Migration = object : Migration(2, 3) {
//            override fun migrate(database: SupportSQLiteDatabase) {
//                database.execSQL("DELETE FROM tb_order")
//                database.execSQL("DELETE FROM tb_device")
//            }
//        }
//
//        val migration_1_3: Migration = object : Migration(1, 3) {
//            override fun migrate(database: SupportSQLiteDatabase) {
//                database.execSQL("ALTER TABLE tb_order ADD COLUMN accountRemainCoin INTEGER NOT NULL DEFAULT 0")
//                database.execSQL("DELETE FROM tb_order")
//                database.execSQL("DELETE FROM tb_device")
//            }
//        }
//
//        val migration_3_4: Migration = object : Migration(3, 4) {
//            override fun migrate(database: SupportSQLiteDatabase) {
//                try {
//                    Log.e("AppDatabase", "开始升级")
//                    database.execSQL("CREATE TABLE IF NOT EXISTS tb_savecoins (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL DEFAULT 0, onDutyCode TEXT, date TEXT, time TEXT, orderNumber TEXT, memberId TEXT, result INTEGER NOT NULL DEFAULT 0, coins INTEGER NOT NULL DEFAULT 0)")
//                    Log.e("AppDatabase", "完成升级")
//                } catch (e: Exception) {
//                    e.printStackTrace()
//                    Log.e("AppDatabase", "升级数据库异常：" + e.message)
//                }
//            }
//        }
//
//        val migration_2_4: Migration = object : Migration(2, 4) {
//            override fun migrate(database: SupportSQLiteDatabase) {
//                database.execSQL("DELETE FROM tb_order")
//                database.execSQL("DELETE FROM tb_device")
//                database.execSQL("CREATE TABLE IF NOT EXISTS tb_savecoins (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL DEFAULT 0, onDutyCode TEXT, date TEXT, time TEXT, orderNumber TEXT, memberId TEXT, result INTEGER NOT NULL DEFAULT 0, coins INTEGER NOT NULL DEFAULT 0)")
//            }
//        }
//
//        val migration_1_4: Migration = object : Migration(1, 4) {
//            override fun migrate(database: SupportSQLiteDatabase) {
//                database.execSQL("ALTER TABLE tb_order ADD COLUMN accountRemainCoin INTEGER NOT NULL DEFAULT 0")
//                database.execSQL("DELETE FROM tb_order")
//                database.execSQL("DELETE FROM tb_device")
//                database.execSQL("CREATE TABLE IF NOT EXISTS tb_savecoins (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL DEFAULT 0, onDutyCode TEXT, date TEXT, time TEXT, orderNumber TEXT, memberId TEXT, result INTEGER NOT NULL DEFAULT 0, coins INTEGER NOT NULL DEFAULT 0)")
//            }
//        }

    }

//    override fun createOpenHelper(config: DatabaseConfiguration): SupportSQLiteOpenHelper {
//
//    }
//
//    override fun createInvalidationTracker(): InvalidationTracker {
//
//    }

    override fun clearAllTables() {

    }

}